﻿using DevComponents.DotNetBar.SuperGrid;
using SDATA;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace SABI
{
    public partial class frm0065 : Form
    {
        public frm0065()
        {
            InitializeComponent();
        }

        DataTable dt_Grid1 = new DataTable();
        bool isNew = false;
        string BranchID = "";

        private void frm0065_Load(object sender, EventArgs e)
        {

        }

        private void frm0065_Shown(object sender, EventArgs e)
        {
            loadDATA();
        }

        void loadDATA(){
            string sql = "SELECT * FROM tb0042 ORDER BY BranchID";
            dt_Grid1 = FRM.GetDataTable(sql);
            GridDB1.PrimaryGrid.DataSource = dt_Grid1;
        }

        private void btnClose_Click(object sender, EventArgs e)
        {
            (Application.OpenForms["frmHome"] as frmHOME).CloseTab(this);
        }

        private void GridDB1_SelectionChanged(object sender, DevComponents.DotNetBar.SuperGrid.GridEventArgs e)
        {
            if (GridDB1.PrimaryGrid.GetSelectedRows().Count < 1) { return; }
            GridRow gr = (GridRow)GridDB1.PrimaryGrid.GetSelectedRows()[0];
            DataRowView drv = (DataRowView)gr.DataItem;
            DataTable dt = dt_Grid1.Clone();
            dt.ImportRow(drv.Row);
            BranchID = dt.Rows[0]["BranchID"].ToString();
            txtBranchID.Text = dt.Rows[0]["BranchID"].ToString();
            txtBranchName.Text = dt.Rows[0]["BranchName"].ToString();
            chkDisabled.Checked = dt.Rows[0]["Disabled"].ToString() == "1" ? true : false;
            isNew = false;
        }

        private void nAdd()
        {
            isNew = true;
            txtBranchID.Text = "";
            txtBranchName.Text = "";
            chkDisabled.Checked = false;
        }

        private void nDelete()
        {
            if (GridDB1.PrimaryGrid.GetSelectedRows().Count < 1)
            {
                SDLL.DLL02.Alert("Bạn phải chọn dòng cần xóa!");
                return;
            }            
            if (!SDLL.DLL02.Confirm("Bạn có chắc muốn xóa?", "Xác nhận")) { return; }
            
            string sql = "DELETE tb0042 WHERE BranchID='{0}'";
            sql = string.Format(sql, BranchID);
            if (DATA.ExecuteNonQuery(sql, FRM.ConnectString))
            {
                loadDATA();
            }
            else
            {
                SDLL.DLL02.Alert("Xóa dữ liệu không thành công");
            }
        }

        private void nInfo()
        {
            string tn = "tb0042";
            string ws = string.Format("WHERE BranchID='{0}'", BranchID);
            Form frm = new frm0067(tn,ws);
            frm.ShowDialog();
        }

        private void nRefresh()
        {
            loadDATA();
        }

        private void btnAdd_Click(object sender, EventArgs e)
        {
            nAdd();
        }      

        private void btnSave_Click(object sender, EventArgs e)
        {
            string sql = "";
            if(isNew)
            {
                sql = "INSERT tb0042(BranchID,BranchName,Disabled,CreateDate,CreateUserID,LastModifyDate,LastModifyUserID)";
                sql += string.Format(" VALUES('{0}',N'{1}','{2}',GETDATE(),'{3}',GETDATE(),'{3}')", txtBranchID.Text, txtBranchName.Text, chkDisabled.Checked?1:0, FRM.UserName);
                if (DATA.ExecuteNonQuery(sql, FRM.ConnectString))
                {
                    SDLL.DLL02.Alert("Lưu dữ liệu thành công");
                    loadDATA();
                }
                else { SDLL.DLL02.Alert("Lưu dữ liệu thất bại"); }
            }
            else
            {
                sql = "UPDATE tb0042 SET BranchID='{0}', BranchName=N'{1}', Disabled='{2}', LastModifyDate=GETDATE(), LastModifyUserID='{3}' WHERE BranchID='{4}'";
                sql = string.Format(sql, txtBranchID.Text, txtBranchName.Text, chkDisabled.Checked?1:0, FRM.UserName, BranchID);
                if (DATA.ExecuteNonQuery(sql, FRM.ConnectString))
                {
                    SDLL.DLL02.Alert("Cập nhật dữ liệu thành công");
                    loadDATA();
                }
                else { SDLL.DLL02.Alert("Cập nhật dữ liệu thất bại"); }
            }
        }

        private void mAdd_Click(object sender, EventArgs e)
        {
            nAdd();
        }

        private void mDelete_Click(object sender, EventArgs e)
        {
            nDelete();
        }

        private void mRefresh_Click(object sender, EventArgs e)
        {
            nRefresh();
        }

        private void mSystemInfo_Click(object sender, EventArgs e)
        {
            nInfo();
        }

    }
}
